package com.dqhl.smartstable.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dqhl.smartstable.R;
import com.dqhl.smartstable.base.BaseActivity;
import com.dqhl.smartstable.model.CarInfo;
import com.dqhl.smartstable.util.Config;
import com.dqhl.smartstable.util.Constant;
import com.dqhl.smartstable.util.Dlog;
import com.dqhl.smartstable.util.JsonUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mac on 16/10/18.
 * 车辆信息
 */
public class CarInfoActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private ImageView iv_top_back;
    private TextView tv_top_center;
    private LinearLayout container;
    private Button btn_add_more;

    private List<CarInfo> carInfoList = new ArrayList<>();

    private boolean isEditable = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_info);
        context = this;

        initView();
        initData();
    }

    private void initData() {
        RequestParams params = new RequestParams(Config.Api.car_info);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)) {
                    carInfoList = JSON.parseArray(data, CarInfo.class);
                    for (int i = 0; i < carInfoList.size(); i++) {
                        final CarInfo carInfo = carInfoList.get(i);
                        final View view = LayoutInflater.from(context).inflate(R.layout.item_car_info, null);
                        EditText et_brand = (EditText) view.findViewById(R.id.et_brand);
                        EditText et_type = (EditText) view.findViewById(R.id.et_type);
                        EditText et_sign = (EditText) view.findViewById(R.id.et_sign);
                        EditText et_is_safe = (EditText) view.findViewById(R.id.et_is_safe);
                        Button btn_delete = (Button) view.findViewById(R.id.btn_delete);
                        Button btn_edit = (Button) view.findViewById(R.id.btn_edit);
                        et_brand.setText(carInfo.getBrand());
                        et_type.setText(carInfo.getType());
                        et_sign.setText(carInfo.getSign());
                        if ("1".equals(carInfo.getIs_safe())) {
                            et_is_safe.setText("是");
                        } else if ("2".equals(carInfo.getIs_safe())) {
                            et_is_safe.setText("否");
                        }
                        btn_delete.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                doDelete(carInfo, view);
                            }
                        });
                        btn_edit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                doEdit(view);
                            }
                        });
                        container.addView(view);
                    }
                } else {
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 编辑
     */
    private void doEdit(View view) {
        EditText et_brand = (EditText) view.findViewById(R.id.et_brand);
        EditText et_type = (EditText) view.findViewById(R.id.et_type);
        EditText et_sign = (EditText) view.findViewById(R.id.et_sign);
        EditText et_is_safe = (EditText) view.findViewById(R.id.et_is_safe);
        String strBrand = et_brand.getText().toString().trim();
        String strType = et_type.getText().toString().trim();
        String strSign = et_sign.getText().toString().trim();
        String isSafe = et_is_safe.getText().toString().trim();
        String strIsSafe = "";
        if ("是".equals(isSafe)) {
            strIsSafe = "1";
        } else if ("否".equals(isSafe)) {
            strIsSafe = "2";
        }
        RequestParams params = new RequestParams(Config.Api.car_edit);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        params.addBodyParameter("motor_brand", strBrand);
        params.addBodyParameter("motor_type", strType);
        params.addBodyParameter("is_safe", strSign);
        params.addBodyParameter("sign", strIsSafe);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)){
                    for (int i = 0; i < carInfoList.size(); i++) {
                        EditText et_brand = (EditText) container.getChildAt(i).findViewById(R.id.et_brand);
                        EditText et_type = (EditText) container.getChildAt(i).findViewById(R.id.et_type);
                        EditText et_is_safe = (EditText) container.getChildAt(i).findViewById(R.id.et_is_safe);
                        et_is_safe.setFocusable(false);
                        et_type.setFocusable(false);
                        et_brand.setFocusable(false);
                        isEditable = false;
                    }
                } else {
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    /**
     * 删除
     * @param carInfo
     */
    private void doDelete(CarInfo carInfo, final View view) {
        Dlog.e("delete-----mortor_id:" + carInfo.getId());
        RequestParams params = new RequestParams(Config.Api.car_delete);
        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
        params.addBodyParameter("motor_id", carInfo.getId());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                String msg = JsonUtils.getMsg(result);
                String code = JsonUtils.getCode(result);
                String data = JsonUtils.getData(result);
                if ("2000".equals(msg)){
                    container.removeView(view);
                } else {
                    toast(code);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        iv_top_back = (ImageView) findViewById(R.id.iv_top_back);
        tv_top_center = (TextView) findViewById(R.id.tv_top_center);
        tv_top_center.setText("车辆信息");
        container = (LinearLayout) findViewById(R.id.container);
        btn_add_more = (Button) findViewById(R.id.btn_add_more);

        iv_top_back.setOnClickListener(this);
        btn_add_more.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_top_back:
                finish();
                break;
//            case R.id.tv_top_right:
//                if (isEditable) {
//                    editCarInfo();
//                } else {
//                    tv_top_right.setText("保存");
//                    for (int i = 0; i < carInfoList.size(); i++) {
//                        EditText et_brand = (EditText) container.getChildAt(i).findViewById(R.id.et_brand);
//                        EditText et_type = (EditText) container.getChildAt(i).findViewById(R.id.et_type);
//                        EditText et_is_safe = (EditText) container.getChildAt(i).findViewById(R.id.et_is_safe);
//                        et_is_safe.setFocusable(true);
//                        et_is_safe.setFocusableInTouchMode(true);
//                        et_is_safe.requestFocus();
//                        et_type.setFocusable(true);
//                        et_type.setFocusableInTouchMode(true);
//                        et_type.requestFocus();
//                        et_brand.setFocusable(true);
//                        et_brand.setFocusableInTouchMode(true);
//                        et_brand.requestFocus();
//                    }
//                    isEditable = true;
//                }
//                break;
            case R.id.btn_add_more:
                addMoreCar();
                break;
        }
    }

    private void addMoreCar() {
        View view = LayoutInflater.from(context).inflate(R.layout.item_car_info, null);
        EditText et_brand = (EditText) view.findViewById(R.id.et_brand);
        EditText et_type = (EditText) view.findViewById(R.id.et_type);
        EditText et_is_safe = (EditText) view.findViewById(R.id.et_is_safe);
        container.addView(view);
//        RequestParams params = new RequestParams(Config.Api.car_add);
//        params.addBodyParameter("token", sp.getString(Constant.TOKEN, ""));
//        params.addBodyParameter("motor_brand", ""); //摩托品牌
//        params.addBodyParameter("motor_type", ""); //摩托型号
//        params.addBodyParameter("is_safe", ""); //是否保险is_safe='1/2:是/否'
//        params.addBodyParameter("sign", ""); //车牌号
//        x.http().post(params, new Callback.CommonCallback<String>() {
//            @Override
//            public void onSuccess(String result) {
//                Dlog.e(result);
//                String msg = JsonUtils.getMsg(result);
//                String code = JsonUtils.getCode(result);
//                String data = JsonUtils.getData(result);
//            }
//
//            @Override
//            public void onError(Throwable ex, boolean isOnCallback) {
//
//            }
//
//            @Override
//            public void onCancelled(CancelledException cex) {
//
//            }
//
//            @Override
//            public void onFinished() {
//
//            }
//        });
    }

}
